// app.js
App({
  onLaunch() {
    // 展示本地存储能力
    const logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    this.globalData.userInfo = wx.getStorageSync('userInfo');
    
    if(!wx.getStorageSync('userInfo')){
        // 登录
        wx.login({
            success: res => {
                // 发送 res.code 到后台换取 openId, sessionKey, unionId
                // https://dx.ikangbai.cn/api/wechat/openid?code=0f3C2N000f9IrU1Y3u000Q5xDP1C2N0w
                wx.request({
                    url: 'https://dx.ikangbai.cn/' + 'api/account/login',
                    method: 'POST',
                    data: { code: res.code },
                    success: res => {
                        console.log('wx_login ....');
                        console.log(res);
                        console.log('wx_login ....');
                        if (res.data.code === 1) {
                            // 3. 存储登录态
                            wx.setStorageSync('token', res.data.data.token)
                            wx.setStorageSync('userInfo', res.data.data.userInfo)
                            
                            // 4. 跳转到首页
                            wx.reLaunch({ url: '/pages/index/index' })
                        }
                    }
                })
            }
        })
    }
  },
  globalData: {
    userInfo: null
  }
})
